package cn.itcast

import java.util.Properties

import org.apache.spark.sql.{DataFrame, SparkSession}

//需要MySQL连接驱动包
object DataFromMysql {
  def main(args: Array[String]): Unit = {
    //1、创建sparkSession对象
    val spark: SparkSession = SparkSession.builder()
      .appName("DataFromMysql")
      .master("local[2]")
      .getOrCreate()
    //2、创建Properties对象，设置连接mysql的用户名和密码
    val properties: Properties =new Properties()
    properties.setProperty("user","root")
    properties.setProperty("password","123456")
    val mysqlDF : DataFrame = spark.read.jdbc("jdbc:mysql://192.168.184.134:3306/spark","person",properties)
    var str = ""
    mysqlDF.collect().foreach(rdd => {
      str = str+rdd.get(0).toString+":"+rdd.get(1).toString
      val id = rdd
    })
    print(str)

    mysqlDF.show()
    spark.stop()
  }
}
